<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar" fixed>
<!--      <template #title>-->
       <van-button
         class="search-btn"
         slot="title"
         type="info"
         size="small"
         round
         icon="search"
         to="/search"
          >搜索</van-button>
<!--      </template>-->
    </van-nav-bar>
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :channel="item"/>
      </van-tab>
      <template #nav-right>
        <div class="placeholder"></div>
        <div class="hamburger-btn" @click="isChannelEditShow = true">
          <i class="font_family icon-gengduo"></i>
        </div>
      </template>
    </van-tabs>
    <van-popup
      v-model="isChannelEditShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <ChannelEdit
        :myChannels="channels"
        :active="active"
        @update-active="onUpdateActive"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from '@/components/article-list.vue'
import ChannelEdit from '@/components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadChannels()
  },
  mounted () {},
  methods: {
    async  loadChannels () {
      try {
        // const { data } = await getUserChannels()
        // this.channels = data.data.channels
        let channels = []
        if (this.user) {
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            channels = localChannels
          } else {
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    onUpdateActive (index, isChannelEditShow = true) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container{
  padding-top: 87px;
  padding-bottom: 50px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn{
      width: 275px;
    height: 32px ;
    background-color: #5babfb;
    border: none;
    font-size: 14px;
    .van-icon {
      font-size: 20px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 46px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 41px;
    }
    .van-tab {
      border-right: 1px solid #EDEFF3;
      min-width: 100px;
      height: 40px;
      font-size: 16px;
      color: #777777;
    }

    .van-tab--active {
      color: #333 !important;
    }
    .van-tabs__line {
      width:16px !important;
      height:3px;
    background-color: #3296fa;
      border-radius:2px;
      bottom: 4px;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .placeholder {
      flex-shrink: 0;//不参与剩余空间计算
      width: 33px;
      height: 41px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 33px;
      height: 41px;
      background-color: #fff;
      opacity: 0.8;
      i.font_family {
        font-size: 16px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
